package com.sxm.yiwei.utils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by Administrator on 2017/10/19.
 * 计算价格
 */

public class Calculator {
    //乘客（客运）
    private static final double PRICE_CAR=0.28;//私家车单价（长途，旅游）
    private static final double PRICE_BUS=0.26;//大巴单价（长途，旅游）
    private static final double START_PRICE_LONG_TRAVEL=20;//长途，旅游起步价
    private static final double PRICE_CITY=1;//乘客同城单价
    private static final double START_PRICE_CITY=5;//同城起步价
    private double sum;//总钱数
    private double single;//单价
    private double startPrice;//起步价
    private double lossMoney=0;//爽约金

    public Calculator() {
    }

    /**
     * 乘客需付钱数(客运)
     * type:0 长途 1 同城 2 旅游
     * tool_type：1 大巴 2 私家车 -1不用分车型
     * distance 距离
     */
    public  double getMoneyByDistance(int type,int tool_type,double distance){
        if (type==0||type==2){
            if (tool_type==1){
                single=PRICE_BUS;
                startPrice=START_PRICE_LONG_TRAVEL;
            }else if (tool_type==2){
                single=PRICE_CAR;
                startPrice=START_PRICE_LONG_TRAVEL;
            }

        }else if (type==1){
            single=PRICE_CITY;
            startPrice=START_PRICE_CITY;
        }
        if (single*distance<=startPrice){
            if (distance==0){//起点和终点一样或者至少有一个地点内容为空
                sum=0;
            }else {
                sum=startPrice;
            }
        }else {
            sum=single*distance;
        }
        return sum;
    }


    /**
     * 爽约金
     * time 单位：h
     */
    public double getLossMoney(double time,double ticketPrice){
        if (time>=48){
            lossMoney=0;
        }else if (time<48&&time>=24){
            lossMoney=ticketPrice*0.05;
        }else if (time<24&&time>=5){
            lossMoney=ticketPrice*0.08;
        }else if (time<5&&time>=0){
            lossMoney=ticketPrice*0.09;
        }else if (time<0&&time>=-0.5){
            lossMoney=ticketPrice*0.10;
        }else {

        }
        return lossMoney;
    }

    /**
     *
     乘客发布长途（旅游）的时候按每公里0.28元去算 ， 最低(起步价)20元（没有起步公里数，不足20元的，按20元计算）
     乘客发布同城的时候起步价5元（含3公里），每公里1元（没有起步公里数，不足5元的，按5元计算）
     1：长途 ; 2:表示旅游；3表示同城
     */

    private static final double PASSER_SINGLE_PRICE_LONG_TOURIST = 0.28;

    private static final double PASSER_SINGLE_PRICE_SAME_CITY = 1.0;

    private static final double OWNER_SINGLE_PRICE_LONG_TOURIST_PERSONAL_CAR = 0.28;

    private static final double OWNER_SINGLE_PRICE_LONG_TOURIST_BUS = 0.26;

    private static final double OWNER_SINGLE_PRICE_SAME_CITY = 1.0;

    public static double getTotalAmountWithPassenger(String pinServiceKinds,double pinTotalDistance){

        double total_amount = 0.0;
        if(pinServiceKinds.equals("1") || pinServiceKinds.equals("2")){
            total_amount = PASSER_SINGLE_PRICE_LONG_TOURIST * pinTotalDistance;
            total_amount = getdoubleAmount(total_amount);
            if(total_amount < 20){

                total_amount = 20.0;
            }
        }else if(pinServiceKinds.equals("3")){

            if(pinTotalDistance < 3){
                total_amount = 5.0;
            }else{
                total_amount = 5.0 + (pinTotalDistance - 3) * PASSER_SINGLE_PRICE_SAME_CITY;
                total_amount = getdoubleAmount(total_amount);
            }

        }

        return total_amount;
    }


    /**
     * 长途、旅游：私家车每公里0.28元，大巴每公里0.26元。 最低(起步价)20元（没有起步公里数，不足20元的，按20元计算）
     * 同城：起步价5元（含3公里），每公里1元（没有起步公里数，不足5元的，按5元计算）
     * pinServiceKinds:出行种类
     * vehicleType:车辆类型,默认为0;1：私家车；2：大巴
     *
     */

    public static double getTotalAmountWithHost(String pinServiceKinds,String vehicleType,double pinTotalDistance){
        double total_amount = 0.0;
        if(pinServiceKinds.equals("1") || pinServiceKinds.equals("2")){
            if(vehicleType.equals("1")){//私家车
                total_amount = OWNER_SINGLE_PRICE_LONG_TOURIST_PERSONAL_CAR * pinTotalDistance ;
                total_amount = getdoubleAmount(total_amount);
                if(total_amount < 20){
                    total_amount = 20.0;
                }
            }else if(vehicleType.equals("2")){//大巴
                total_amount = OWNER_SINGLE_PRICE_LONG_TOURIST_BUS * pinTotalDistance ;
                total_amount = getdoubleAmount(total_amount);
                if(total_amount < 20){
                    total_amount = 20.0;
                }
            }

        }else if(pinServiceKinds.equals("3")){
            if(pinTotalDistance < 3){
                total_amount = 5.0;
            }else{
                total_amount = 5.0 + (pinTotalDistance - 3) * OWNER_SINGLE_PRICE_SAME_CITY;
                total_amount = getdoubleAmount(total_amount);
            }
        }

        return total_amount;
    }


    /**
     * 四舍五入,保留一位小数
     */

    public static double getdoubleAmount(double f){
        double total_amount = 0.0;

        BigDecimal b = new BigDecimal(f);
        total_amount = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();

        return total_amount;
    }

   /*
   *
   * flag:0表示改签赔付原车主的总金额  1：表示退还金额
   * total_amount：改签票数*原订单单价
   *
   * */
    public static double getChangePayBack(double total_amount,int flag){
         double payment=0.0;//表示改签赔付原车主的总金额
        double quit_amount=0.0;//表示改签赔付原车主的总金额

        payment=total_amount*0.05;
        quit_amount=total_amount-payment;
        payment=getdoubleAmount(payment);
        quit_amount=getdoubleAmount(quit_amount);
        if (flag==0){
            return payment;
        }else {
            return quit_amount;
        }


    }
    /*

    * 获取退票补偿金
    * flag: 0赔付金额 1 退还金额
    * flag两种情况只对乘客有效
    * 对于车主只有flag=0有效
    * */
    public static double getPaymentAmount(Date leaveOffTime, double total_amount,int flag){
        double payment = 0.0;
        double payback=0.0;
        long leaveMilliSeconds = leaveOffTime.getTime(); //出发时间
        long currentMilliSeconds = System.currentTimeMillis();//当前时间
        if( currentMilliSeconds+ 48 * 60 * 60 * 1000 < leaveMilliSeconds){//发车前48小时以上退票，不扣费
            payment = 0;

        }else if(currentMilliSeconds + 48 * 60 * 60 * 1000 > leaveMilliSeconds && currentMilliSeconds + 24 * 60 * 60 * 1000 < leaveMilliSeconds){
            payment = total_amount * 0.05;

        }else if(currentMilliSeconds + 24 * 60 * 60 * 1000 > leaveMilliSeconds && currentMilliSeconds + 5 * 60 * 60 * 1000 < leaveMilliSeconds){
            payment = total_amount * 0.08;

        }else if(currentMilliSeconds + 5 * 60 * 60 * 1000 > leaveMilliSeconds){
            payment = total_amount * 0.09;
        }
        if (flag==0){
            return getdoubleAmount(payment);
        }else {
            payback=total_amount-payment;
            return getdoubleAmount(payback);
        }

    }






}
